package cn.tede.javagame.shoot;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.List;  //接口
import java.util.ArrayList;


public class ShootPanel extends JPanel implements MouseMotionListener {
    //属性
    Image bgImage = new ImageIcon("pjdz/Pic/background.jpg").getImage();//背景图片属性
    int bgY = 0;
    //英雄机属性
    cn.tede.javagame.shoot.Hero hero = new cn.tede.javagame.shoot.Hero();
    //存储子弹的集合
    List<Bullet>bullets= new ArrayList<>(); //向上造型
    //存储敌人的集合
    List<Enermy>enermys= new ArrayList<>();
    //敌机属性
    Enermy enermy = new Enermy(); //生成单个敌机
    //Enermy[] enermys = new Enermy[3]; //生成多个敌机
    //构造
    public ShootPanel(){
        //鼠标事件的监听注册
        this.addMouseMotionListener(this);
        //for(int i =0;i<enermys.length;i++){
            //enermys.add(new Enermy());
        //}
    }
    //重写父类的paint方法
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //绘制背景图片
        paintBackground(g);
        //绘制英雄机对象
        paintHero(g);
        //绘制分数，生命值等数据
        paintScore(g);
        //绘制敌人
        paintEnemy(g);
        //绘制子弹
        paintBullet(g);
    }
    Image gg = new ImageIcon("pjdz/Pic/gg.png").getImage();
    //绘制背景图片
    public void paintBackground(Graphics g){
        g.drawImage(bgImage,0,bgY,null);
        g.drawImage(bgImage,0,bgY- ShootFrame.HEIGHT,null);
    }
    //绘制英雄机对象
    public void paintHero(Graphics g){
        g.drawImage(hero.heroImg,hero.x,hero.y,null);
    }
    //绘制分数对象
    public void paintScore(Graphics g){
        g.drawImage(gg,5,660,null);
        g.setColor(Color.RED);
        Font f = new Font("宋体",Font.BOLD,28);
        g.setFont(f);
        g.drawString("得分:"+hero.score,350,710);
        g.drawString("金币:"+hero.gold,230,710);
        g.drawString("生命:"+hero.life,80,710);
        g.setColor(Color.GREEN);
        g.drawString("关卡："+(hero.score/1000+1),370,30);
    }
    //绘制子弹
    //paintBullet(g);
    //绘制敌人对象
    /*
   public void paintEnermy(Graphics g){
       for(int i =0;i<enermys.size();i++){
           Bullet bullet=enermys.get(i);
           g.drawImage(bullet.enermysImg,enermys.x,bullet.y,null);
       }
//        for(int i =0;i<enermys.length;i++){
//            g.drawImage(enermys[i].enemyImag,enermys[i].x,enermys[i].y,null);
//        }
        //g.drawImage(enermy.enemyImag,enermy.x,enermy.y,null);
    }
     */
    //自定义方法，表示游戏启动
    public void action(){
        //创建定时器对象
        Timer timer = new Timer();
        //启动定时器
        timer.schedule(new TimerTask(){
            public void run(){
                //背景图片的移动
                backgroundMove();
                //产生敌机
                enemyAction();
                //英雄机的动态
                hero.step();
                //产生子弹
                bulletAction();
                //移动子弹
                bulletMove();
                //移动敌机
                enemyMoveAndStep();
                //越界的处理
                bulletOutOfBounds();
                //敌机的越界

                //判断敌机和子弹的位置
                for(int i=0;i<bullets.size();i++){
                    Bullet bullet = bullets.get(i);
                    for(int j=0;j<enermys.size();j++){
                        Enermy enermy = enermys.get(j);
                        if(bullet.hitEnermy(enermy)){
                            if (enermy.life==0){
                                enermys.remove(enermy);
                                j--;
                                hero.score+=100;
                            }
                            bullets.remove(bullet);
                            i--;
                            break;
                        }
                    }
                }
                //敌机和英雄的判断
                for(int i=0;i<enermys.size();i++){
                    Enermy enermy = enermys.get(i);
                    if(enermy.x){
                }

                //重绘
                repaint();
            }
        },0,10);
    }
    //产生子弹
    int bulletIndex = 0;
    public void bulletAction(){
        bulletIndex++;
        if(bulletIndex>=Integer.MAX_VALUE){bulletIndex=0;}
        if(bulletIndex%30==0){
            bullets.addAll(hero.shoot());
        }
    }
    //产生敌人
    int enermyIndex = -1;
    public void enemyAction(){
        enermyIndex++;
        if(enermyIndex%40==0) {
            enermys.add(new Enermy());
        }
    }
    //移动子弹
    public void bulletMove(){
        for(int i =0;i<bullets.size();i++){
            Bullet bullet = bullets.get(i);
            bullet.move();
        }
    }
    //移动敌人
    public void enemyMoveAndStep(){
        for(int i =0;i<enermys.size();i++){
            Enermy enermy = enermys.get(i);
            enermy.move();
            enermy.step();
        }
    }
    //绘制子弹
    public void paintBullet(Graphics g){
        for(int i =0;i<bullets.size();i++){
            Bullet bullet=bullets.get(i);
            g.drawImage(Bullet.bulletImg,bullet.x,bullet.y,null);
        }
    }
    //绘制敌人
    public void paintEnemy(Graphics g){
        for(int i =0;i<enermys.size();i++){
            Enermy enermy = enermys.get(i);
            g.drawImage(Enermy.enemyImag,enermy.x,enermy.y,null);

        }
    }
    //子弹越界处理
    public void bulletOutOfBounds(){
        for(int i =0;i<bullets.size();i++){
            Bullet bullet=bullets.get(i);
            if(bullet.y<0){
                bullets.remove(i);
            }
        }

    }
    //敌人越界处理
    public void enemyOutOfBounds(){
        for(int i =0;i<enermys.size();i++){
            Enermy enermy = enermys.get(i);
            if(enermy.y>ShootFrame.HEIGHT){
                enermys.remove(i);
            }
        }
    }
    //敌人趋势
    public void EnermyDead(){

    }
    public boolean hitEnermy(Bullet bullet,Enermy enermy){
        if(bullet.x>=enermy.x||bullet.x<enermy.x+30){
            enermy.life-=100;
        }
        if(enermy.life==0){
            enermys.remove(enermy);
        }
        return true;
    }

    public void backgroundMove(){
        bgY++;
        if(bgY>=ShootFrame.HEIGHT){bgY=0;}
    }
    @Override
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        hero.move(e.getX()-hero.heroImg.getWidth(null)/2,
                  e.getY()-hero.heroImg.getHeight(null)/2);
    }
}

